package rlpw.swing.table;

import java.awt.Color;
import java.awt.Component;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import rlpw.lang.StringParser;
import rlpw.swing.ColorIcon;

public class ColorRenderer extends DefaultTableCellRenderer {

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
            int row, int column) {
        super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

        Color color = (Color) value;
        setIcon(new ColorIcon(color));
        String colorName = StringParser.getString(color);

        if (!colorName.startsWith("#")) {
            colorName = colorName.toLowerCase();
        }

        setText(colorName);
        setToolTipText("(r=" + color.getRed() + ", g=" + color.getGreen() + ", b=" + color.getBlue() + ")");

        return this;
    }
}